package cn.jbooter.shiro.autoconfigure.config;

import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;

import cn.jbooter.shiro.autoconfigure.cache.SpringCacheManagerWrapper;
import cn.jbooter.shiro.autoconfigure.properties.IShiroProperties;

/**
 * Shiro的缓存管理器配置
 * @author HeJian
 *
 */
@ConditionalOnProperty(prefix="spring.shiro.cacheManager",name="enabled",havingValue="true")
public class ShiroCacheManagerConfiguration {
	
	@Autowired
	protected IShiroProperties shiroProperties;
	/**
     * 默认的缓存管理器配置,使用shiro的ehcache实现
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public CacheManager shiroModeCacheManager() {
    	EhCacheManager cm = new EhCacheManager();
    	cm.setCacheManagerConfigFile(shiroProperties.getCacheManager().getShiroCacheManagerConfigFile());
    	return cm;
    }
    
    /**
     * 使用spring提供的缓存管理器配置
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix="spring.shiro.cacheManager",name="cacheMode" ,havingValue="spring")
    public CacheManager springModeCacheManager(org.springframework.cache.CacheManager cacheManager) {
    	SpringCacheManagerWrapper shiroCacheManager = new SpringCacheManagerWrapper();
    	shiroCacheManager.setCacheManager(cacheManager);
    	return shiroCacheManager;
    }
}
